<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文本编辑</title>
    <!--全屏-->
    <link href="./fullscreen/wangEditor-fullscreen-plugin.css" rel="stylesheet" />
</head>
<body>
    <div id="editor">
        <!--<p>欢迎使用 <b>wangEditor</b>富文本编辑器</p>
        <button id="btn1">获取html</button>
		<button id="btn2">获取text</button>-->
		<p>请稍等...</p>
    </div>
	<!--jQ-->
	<script type="text/javascript" src="./jquery-2.0.0.min.js"></script>
    <!-- 注意， 只需要引用 JS，无需引用任何 CSS ！！！-->
    <script type="text/javascript" src="./wangEditor.min.js"></script>
    <!--全屏-->
    <script type="text/javascript" src="./fullscreen/wangEditor-fullscreen-plugin.js"></script>
    
    <script type="text/javascript">
    	
    	/*
    	 * 初始化编辑器
    	 */
        var E = window.wangEditor
        var editor = new E('#editor')
         // 隐藏“网络图片”tab
    	editor.customConfig.showLinkImg = false
    	// 使用 base64 保存图片
    	editor.customConfig.uploadImgShowBase64 = true   
		//初始化
        editor.create();
        //全屏功能
        E.fullscreen.init('#editor');
        //请求参数
		var tP = GetQueryString("tP");
		//请求路径
		var reqUrl = GetQueryString("reqUrl");
		//cookie
		var cs = GetQueryString("cs");
		//reqUrl = "http://172.18.8.254:8080";
		if(tP != '' && reqUrl != ''){
			get();
		}else{
			// 禁用编辑
			editor.$textElem.attr('contenteditable', false)
		}
        
        /**
         * 获取数据
         */
        function get(){
        	var param = {tP:tP}
        	$.ajax({
                url: reqUrl + 'textEditor/get/api',
                type: 'POST',
                contentType: "application/json", //必须这样写
		        dataType:"json",
		        data:JSON.stringify(param),//要提交是json字符串
                success: function (returndata) {
                    console.log(returndata.msg);
                    if(returndata.result){
                    	if(!returndata.data.text){
                    		returndata.data.text = '';
                    	};
                    	//赋值
                    	editor.txt.html(returndata.data.text);
                    	//添加菜单栏保存按钮
				        var editorSelector = '#editor';
				        $(editorSelector + " .w-e-toolbar").append('<div class="w-e-menu"><a class="_wangEditor_btn_save" href="#" onclick="save(\'' + editorSelector + '\')">保存</a></div>');
                    }else{
                    	// 禁用编辑
						editor.$textElem.attr('contenteditable', false)
                    }
                },
                error: function (returndata) {
                    console.log('错误');
                    console.log(returndata.msg);
                }
          	});
        }
        
        /**
         * 保存
         */
        function save(editorSelector){
        	if($(editorSelector + ' ._wangEditor_btn_save').text() != '保存'){
        		alert('正在保存,请稍等...')
        	}
        	$(editorSelector + ' ._wangEditor_btn_save').text('保存中...');
        	var param = {tP:tP,text:editor.txt.html()};
        	$.ajax({
                url: reqUrl + 'textEditor/save/api' ,
                type: 'POST',
                contentType: "application/json", //必须这样写
		        dataType:"json",
		        data:JSON.stringify(param),//要提交是json字符串
                success: function (returndata) {
                	if(returndata.result){
                		$(editorSelector + ' ._wangEditor_btn_save').text('保存成功');
                    }else{
                    	$(editorSelector + ' ._wangEditor_btn_save').text('保存失败');
                    }
                	console.log(returndata.msg);
                    setTimeout(function(){$(editorSelector + ' ._wangEditor_btn_save').text('保存')}, 3000);
                },
                error: function (returndata) {
                    $(editorSelector + ' ._wangEditor_btn_save').text('保存');
                    console.log('错误');
                    console.log(returndata.msg);
                }
          	});
        	
        }
        //获取url参数
        function GetQueryString(name) { 
			  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
			  var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
			  var context = ""; 
			  if (r != null) 
			     context = r[2]; 
			  reg = null; 
			  r = null; 
			  return context == null || context == "" || context == "undefined" ? "" : context; 
		}
        /**
         * 演示
         */
//	    document.getElementById('btn1').addEventListener('click', function () {
//	        // 读取 html
//	        alert(editor.txt.html())
//	    }, false)
//	
//	    document.getElementById('btn2').addEventListener('click', function () {
//	        // 读取 text
//	        alert(editor.txt.text())
//	        
//	    }, false)
//		
        
    </script>
</body>
</html>